#include<bits/stdc++.h>
using namespace std;
typedef struct TNode * Position;
typedef Position Bintree;
struct TNode{
	int data;
	Bintree lchild;
	Bintree rchild;
};
Bintree Insert(Bintree BT,int x){
	if(!BT){
		BT = new TNode;
		BT->data = x;
		BT->lchild = BT->rchild = NULL;
	}
	else {
		if(x<=BT->data)
			BT->lchild = Insert(BT->lchild,x);
		else 
			BT->rchild = Insert(BT->rchild,x);
	}
	return BT;
}
int find(Bintree &BT){
	Bintree T,pre;
	T = BT;pre = NULL;
	while(1){
		if(T->lchild){
			pre = T;
			T = T->lchild;
		}
		else {
			if(T == BT)BT = T->rchild;
			else pre->lchild = T->rchild;
			return T->data;
		}
	}
}
int main(void){
	int n,i;
	int data;
	int e1,e2,sum = 0;
	Bintree BT = NULL;
	cin>>n;
	for(i = 0;i < n ;i++){
		cin>>data;
		BT = Insert(BT,data);
	}
	while(n-1){
		e1 = find(BT);
		e2 = find(BT);
		sum+=e1+e2;
		BT = Insert(BT,e1+e2);
		n--;
	}
	cout<<sum;
	return 0;
}
